import { Action } from '@ngrx/store';
import { User } from '../entity/user.model';
import * as UserActions from '../actions/user.action';

const initUser= {
  userId: null,
  username: null,
  gender: null,
  age: null,
  faceInfo: null,
  idState: 0,
  idInfo: null,
  bankState: 0,
  bankInfo: null,
  mobileState: 0,
  mobileInfo: null,
  zmxyState: 0,
  workState: 0,
  workInfo: null,
  moreState: 0,
  moreInfo: null,
  contactState: 0,
  contacts: null,
}
/*** 用户信息相关
 * */
export function UserReducer (state: User= initUser, action: UserActions.All) {
  switch (action.type) {

    /** 认证中心个人信息
     * 0 退出清空状态
     * */
    case UserActions.RESET_AUTHENTICATE_STORE:
      return initUser;

    /** 认证中心个人信息
     * 1.0 初始化认证中心状态
     * */
    case UserActions.SAVE_AUTHENTICATE_STATUS:
      let userMain= action.payload.userMain || {}; // 主表
      let userInfo= action.payload.userInfo || {}; // 分表
      const { objectId, username, sex, age,
        idState , realname, idNo, education,
        contactState,
        bankState,
        mobileState,
        zmxyState,
        workState,
        moreState,
      }= userMain;
      const { pass, compareFaceVal, livingImg, frontImg, backImg, liveAddr,
        contact,
        bank, bankNo, bankphone, creditCard, creditCardNo, creditphone,
        companyName, companyPhone, companyAddr, workingYears, workingImg,
        taobao, email, qq, wechat,
      }= userInfo;

      return {...state,
        userId: objectId ||  '', // 用户id
        username: username ||  '', // 用户名称
        gender:sex ||  '', // 性别
        age: age ||  '', // 年龄
        faceInfo: {
          pass: pass || 0, // 比对状态
          compareFaceVal: compareFaceVal || '', // 人脸和身份证对比值
        },
        idState: idState || 0, // 身份认证
        idInfo: {
          livingImg: livingImg || '', // 人脸活动拍的照片
          frontImg: frontImg || '', // 身份证正面
          backImg: backImg|| '', // 身份证背面
          realname: realname || '', // 真实姓名
          idNo: idNo || '', // 身份证号
          education: education|| '', // 学历
          liveAddress: liveAddr|| '', // 现居住地址
        },
        contactState: contactState || 0, // 联系人是否已保存过
        contacts: contact?JSON.parse(contact): '', // 联系人相关
        bankState: bankState || 0, // 银行卡认证状态
        bankInfo: {
          bankName: bank || '', // 银行名称
          bankNo: bankNo || '',   // 银行卡号
          bankphone: bankphone || '', // 银行预留手机号
          creditCard: creditCard || '', // 信用卡银行名称
          creditCardNo: creditCardNo || '', // 信用卡银行卡号
          creditphone: creditphone || '', // 信用卡银行预留手机号
        },
        mobileState: mobileState || 0, // 手机运营商状态
        mobileInfo: {
          collect_website: '', // 号码归属地
          token: '', // 三方token
          // plan: 0, // 验证进度中 0 初始化 1 需要验证码
        },
        zmxyState: zmxyState || 0, // 芝麻信用认证状态
        workState: workState || 0, // 工作信息认证状态
        workInfo: {
          companyName: companyName || '', // 公司姓名
          companyPhone: companyPhone || '', // 公司电话
          companyAddr: companyAddr || '', // 公司地址
          workingYears: workingYears || '', // 工作年限
          workingImg: workingImg || [], // 工作照片
        },
        moreState: moreState || 0, // 更多信息认证状态
        moreInfo: {
          taobao: taobao || '', // 淘宝账号
          email: email || '', // 邮箱
          qq: qq || '', // qq
          wechat: wechat || '', // 微信
        },
      };

    /** 认证中心个人信息
     * 1.1 更新身份证信息
    * */
    case UserActions.UPDATE_CARD_INFO:
      const { name ,cardno }= action.payload;
      return {...state,
        idInfo: { ...state.idInfo,
          realname: name,
          idNo: cardno,
        }
      };

    /** 认证中心个人信息
     * 1.2 更新比对阈值
     * */
    case UserActions.UPDATE_FACE_CONTRAST:
      return {...state,
        faceInfo: { ...state.faceInfo,
          pass: action.payload.pass,
          compareFaceVal: action.payload.data,
        },
      };

    /** 认证中心个人信息
     * 1.3 绑定成功,更新idState
     * */
    case UserActions.BIND_USER_INFO_SUCCESS:
      const data= action.payload;
      return {...state,
        idState: data.idState || 1,
        idInfo: { ...state.idInfo,
          education: data.education,
          liveAddress: data.liveAddr,
          livingImg: data.livingImg,
          frontImg: data.frontImg,
          backImg: data.backImg,
        }
      };

    /** 认证中心个人信息
     * 1.4 绑定成功后,仅更新学历和地址信息
     * */
    case UserActions.UPDATE_USER_INFO:
      return {...state,
        idInfo: { ...state.idInfo,
          education: action.payload.education,
          liveAddress: action.payload.liveAddr,
        }
      };

    /** 认证中心联系人信息
     * 2.1 更新联系人
     * */
    case UserActions.UPDATE_CONTACTS:
      return {...state,
        contactState: action.payload.contactState,
        contacts: { ...state.contacts,...action.payload.contacts}
      };

    /** 认证中心绑定银行卡
     * 3.1 绑定银行卡成功
     * */
    case UserActions.BIND_BANK_CARD_SUCCESS:
      return {...state,
        bankState: action.payload.bankState,
        bankInfo: {...state.bankInfo,...action.payload.bankInfo}
      };

    /** 认证中心绑定运营商授权
     * 4.1 获取运营商token
     * */
    case UserActions.GET_MOBILE_TOKEN:
      return {...state,
        mobileInfo: {...state.mobileInfo,
          token: action.payload.token,
          collect_website: action.payload.collect_website,
        }
      };

    /** 认证中心绑定运营商授权
     * 4.2 可能需要获取短信验证码
     * */
    // case UserActions.NEED_SEND_MSG_CODE:
    //   debugger;
    //   return {...state,
    //     mobileInfo: {...state.mobileInfo,
    //       plan: action.payload.plan,
    //     }
    //   };

    /** 认证中心绑定运营商授权
     * 4.2 运营商认证成功
     * */
    case UserActions.BIND_TELEPHONE_SUCCESS:
      return {...state,
        mobileState: action.payload.mobileState,
      };

    /** 认证中心绑定工作信息
     * 6.1 上传工作照成功
     * */
    case UserActions.UPLOAD_WORK_IMGS_SUCCESS:
      return {...state,
        workInfo: { ...state.workInfo,
          workingImg: action.payload.workingImg,
        }
      };

    /** 认证中心绑定工作信息
     * 6.2 保存工作信息成功
     * */
    case UserActions.UPDATE_WORK_INFO_SUCCESS:
      return {...state,
        workState: action.payload.workState,
        workInfo: { ...state.workInfo,
          workingImg: action.payload.workingImg
        }
      };

    /** 认证中心绑定更多信息
     * 7.1 更新更多信息
     * */
    case UserActions.UPDATE_MORE_INFO:
      return {...state,
        moreState: action.payload.moreState,
        moreInfo: { ...state.moreInfo,...action.payload.moreInfo}
      };

    default:
      return state;
  }
}
